﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SmartProject.BusinessObjects;

namespace SmartProject.AppSpace
{
    public partial class Sprints : UI.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                base.SetPage("Sprint Cycles", "Ycan view the burndown chart for each sprint cycle or add/modify/remove sprint cycle.");
                RefreshPage(new SmartProject.BusinessObjects.Sprint());
            }
        }

        protected void DrpSprints_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.DrpSprints.SelectedIndex > 0)
            {
                this.ImgBurndownChart.ImageUrl = String.Format("~/AppSpace/Download.aspx?ID={0}&Action=BurndownChart",
                    this.DrpSprints.SelectedValue);
                this.ImgBurndownChart.Visible = true;
                this.LblDays.Visible = true;
                this.LblHours.Visible = true;
            }
            else
            {
                this.ImgBurndownChart.Visible = false;
                this.LblDays.Visible = false;
                this.LblHours.Visible = false;
            }
        }

        protected void BtnEdit_Click(object sender, EventArgs e)
        {
            this.LblSprintDesc.Text = "Update Sprint Cycle";
            Sprint sprint = new Sprint();
            sprint.SetAttribute("SprintId", this.DrpSprints.SelectedItem.Value);
            sprint.Load();
            this.TxtSprintStart.Text = sprint.GetAttributeDateTime("Started").ToShortDateString();
            this.TxtSprintEstDays.Text = sprint.GetAttributeValue("EstimatedDays").ToString();
            this.TxtSprintDescription.Text = sprint.GetAttributeValue("Description").ToString();
            this.BtnSaveSprint.CommandArgument = this.DrpSprints.SelectedItem.Value;
            this.PopSprint.Show();
        }

        public override void RefreshPage(object obj)
        {
            Sprint sprint = (Sprint)obj;
            sprint.Populate(this.DrpSprints);

            if (this.DrpSprints.Items.Count > 0)
            {
                this.DrpSprints.Items.Insert(0, new ListItem("Select one", "0"));
                this.DrpSprints.Enabled = true;
            }
            else
            {
                this.DrpSprints.Enabled = false;
            }
            this.ImgBurndownChart.Visible = false;
            this.LblDays.Visible = false;
            this.LblHours.Visible = false;
        }

        protected void BtnRemove_Click(object sender, EventArgs e)
        {
            bool isFirstTimeManagementSetup = IsFirstTimeManagementSetup;
            Sprint sprint = new Sprint();
            sprint.SetAttribute("SprintId", Convert.ToInt32(this.DrpSprints.SelectedValue));
            sprint.Remove();

            if (isFirstTimeManagementSetup)
            {
                // Force this page to refresh so that the links in the
                // dashboard can be shown.
                Response.Redirect("~/AppSpace/Management.aspx", true);
            }
            else
            {
                if (this.DrpSprints.Items.Count == 1)
                {
                    Response.Redirect("~/AppSpace/Management.aspx", true);
                    return;
                }
                RefreshPage(sprint);
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            this.LblSprintError.Text = String.Empty;
            Sprint sprint = new Sprint();
            try
            {
                sprint.SetAttribute("Description", this.TxtSprintDescription.Text);
                sprint.SetAttribute("Started", this.TxtSprintStart.Text);
                sprint.SetAttribute("EstimatedDays", this.TxtSprintEstDays.Text);
            }
            catch (Exceptions.RequiredDataParameter requiredDataParameter)
            {
                this.LblSprintError.Text = requiredDataParameter.Message;
                this.PopSprint.Show();
                return;
            }
            catch (Exceptions.InvalidDataParameter invalidDataParameter)
            {
                this.LblSprintError.Text = invalidDataParameter.Message;
                this.PopSprint.Show();
                return;
            }

            bool isFirstTimeManagementSetup = IsFirstTimeManagementSetup;
            if (String.IsNullOrEmpty(this.BtnSaveSprint.CommandArgument))
            {
                sprint.SetAttribute("OwnerId", base.UserId);
                try
                {
                    sprint.Save();
                }
                catch (Exceptions.BusinessRuleViolation businessRuleViolation)
                {
                    this.LblSprintError.Text = businessRuleViolation.Message;
                    this.PopSprint.Show();
                    return;
                }
            }
            else
            {
                sprint.SetAttribute("SprintId", Convert.ToInt32(this.BtnSaveSprint.CommandArgument));
                try
                {
                    sprint.Update();
                }
                catch (Exceptions.BusinessRuleViolation businessRuleViolation)
                {
                    this.LblSprintError.Text = businessRuleViolation.Message;
                    this.PopSprint.Show();
                    return;
                }
            }

            this.BtnSaveSprint.CommandArgument = String.Empty;

            if (isFirstTimeManagementSetup)
            {
                // Force this page to be refresh so that the links in the
                // dashboard can be shown.
                Response.Redirect("~/AppSpace/Management.aspx", true);
            }
            else
            {
                RefreshPage(sprint);
            }
        }

        public override AjaxControlToolkit.ModalPopupExtender Popup(string name)
        {
            return null;
        }
    }
}
